<?php

namespace App\Console\Commands;

use App\Http\Common\constcodes\ConstCodes;
use App\Http\Common\pools\mysql\MysqliConfigPool;
use App\Http\Common\pools\redis\RedisConfigPool;
use App\Http\Models\CallLog\CallLogModel;
use Illuminate\Console\Command;
use Swoole\Coroutine;
use Vtiful\Kernel\Excel;
use function Swoole\Coroutine\run;

class ExportXlsxCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:export {--M|method= : import|export|read}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $method = $this->option('method');
        $config = [
            'path' => './Temp/' // xlsx文件保存路径
        ];
        $fileName  = 'tutorial01.xlsx';
        $header = ['Item', 'Cost'];
        $excel = new Excel($config);
        switch ($method) {
            case ConstCodes::XLSX_EXPORT:
                $filePath = $excel->fileName($fileName, 'test1')
                    ->header($header)
                    ->data([
                        ['Rent', 1000],
                        ['Gas',  100],
                    ])
                    // 第二个sheet
                    ->addSheet('test2')
                    ->header($header)
                    ->data([
                        ['Food', 300],
                        ['Gym',  50],
                    ])
                    ->output();
                break;
            case ConstCodes::XLSX_IMPORT:
                break;
            case ConstCodes::XLSX_READER:
                // 多sheet例子
                $sheetList = $excel->openFile('tutorial.xlsx')
                    ->sheetList();
                # foreach ($sheetList)

                // 读取测试文件
                $data = $excel->openFile($fileName)
                    ->openSheet()
                    ->getSheetData();

                var_dump($data); // [['Item', 'Cost']]
                break;
            case 'exportAll':
                $this->exportAll();
                break;
            default:
                throw new \Exception('not support methods');
        }
        $excel->close();
    }

    public function exportAll()
    {
        run(function () {
            $redisPool = new RedisConfigPool();
            Coroutine::create(function () use($redisPool) {
                $instance = new MysqliConfigPool('db_v2');
                $con = $instance->get();
                $redis = $redisPool->get();
                $con->table('call_log')->where('id', '>', 22900)->chunkById(1000, function ($callLogs) use ($redis) {
                    /**
                     * @var CallLogModel[] $callLogs
                     */
                    foreach ($callLogs as $callLog) {
                        $redis->lpush('call-log-test', json_encode($callLog));
                    }
                });
            });

            Coroutine::create(function () use($redisPool) {
                $redis = $redisPool->get();
                $config = [
                    'path' => './Temp/' // xlsx文件保存路径
                ];
                $fileName  = 'callLog.xlsx';
                $excel = new Excel($config);
                $fileObject = $excel->fileName($fileName, 'Sheet1');
                while($redis->exists('call-log-test')) {
                    $fileObject->data(json_decode($redis->rpop('call-log-test'), true));
                }
                $fileObject->output();

            });
        });
    }
}
